package com.nutstudio.feilproject.presenter;

import android.support.annotation.NonNull;

import com.nutstudio.feilproject.bean.TestBean;
import com.nutstudio.feilproject.manager.DataManager;
import com.nutstudio.feilproject.manager.net_tools.UploadListener;
import com.nutstudio.feilproject.net.UploadRetrofitHelper;
import com.nutstudio.feilproject.view.TestView;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class MainPresenter extends BasePresenter {
    private TestView expressView;
    private DataManager dataManager;

    public MainPresenter(TestView expressView, LifecycleProvider<ActivityEvent> provider) {
        super(provider);
        this.expressView = expressView;
        dataManager = DataManager.getInstance();
    }

     /**
     * 获取快递信息
     *
     * @param code   快递类型
     */
    public void getTestInfo(int code) {
        expressView.showProgressDialog();

        dataManager.getTestInfo(code)
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .compose(getProvider().<TestBean>bindUntilEvent(ActivityEvent.DESTROY)) // onDestroy取消订阅
                .subscribe(new DefaultObserver<TestBean>() {  // 订阅
                    @Override
                    public void onNext(@NonNull TestBean expressInfo) {
                        expressView.updateView(expressInfo);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        expressView.showError(e.getMessage());
                        expressView.hideProgressDialog();
                    }

                    @Override
                    public void onComplete() {
                        expressView.hideProgressDialog();
                    }
                });
    }

    /**
     *
     * @param code
     */
    public void getPostTestInfo(int code) {
        expressView.showProgressDialog();

        dataManager.postTestInfo(code)
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .compose(getProvider().<TestBean>bindUntilEvent(ActivityEvent.DESTROY)) // onDestroy取消订阅
                .subscribe(new DefaultObserver<TestBean>() {  // 订阅
                    @Override
                    public void onNext(@NonNull TestBean expressInfo) {
                        expressView.updateView(expressInfo);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        expressView.showError(e.getMessage());
                        expressView.hideProgressDialog();
                    }

                    @Override
                    public void onComplete() {
                        expressView.hideProgressDialog();
                    }
                });
        File file=new File("");
        Observable.just(file).map(new Function<File, String>() {
            @Override
            public String apply(File file) throws Exception {


                return null;
            }
        }).

        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                //TODO 得到所有文件，得到总个数

                //循环遍历发射
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onNext(3);
            }
        }).map(new Function<Integer, String>() {
            @Override
            public String apply(Integer integer) throws Exception {

                return null;
            }
        }).flatMap(new Function<Integer, ObservableSource<String>>() {
            @Override
            public ObservableSource<String> apply(Integer integer) throws Exception {

                return dataManager.getTestInfo(integer);
            }
        }).subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {

            }
        });
    }


    public void uploadFile(String des, File file1,File file2) {
        List<File> fileList=new ArrayList<>();
        fileList.add(file1);
        fileList.add(file2);
        expressView.showProgressDialog();

        dataManager.uploadFiles(des,fileList)
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .compose(getProvider().<TestBean>bindUntilEvent(ActivityEvent.DESTROY)) // onDestroy取消订阅
                .subscribe(new DefaultObserver<TestBean>() {  // 订阅
                    @Override
                    public void onNext(@NonNull TestBean expressInfo) {
                        expressView.updateView(expressInfo);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        expressView.showError(e.getMessage());
                        expressView.hideProgressDialog();
                    }

                    @Override
                    public void onComplete() {
                        expressView.hideProgressDialog();
                    }
                });
    }
    public void uploadFileS(String des, File file1, UploadListener uploadListener) {


        List<File> fileList=new ArrayList<>();
        fileList.add(file1);
        Map<String, RequestBody> maps = new HashMap<>();
        for (int i = 0; i < fileList.size(); i++) {
            File file = fileList.get(i);
            maps.put("file\";filename=\"" + file.getName(), RequestBody.create(MediaType.parse("image/png"), file));
        }

        expressView.showProgressDialog();
        UploadRetrofitHelper.getInstance(uploadListener).getRetrofitService()
        .uploadFiles(des,maps)
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .compose(getProvider().<TestBean>bindUntilEvent(ActivityEvent.DESTROY)) // onDestroy取消订阅
                .subscribe(new DefaultObserver<TestBean>() {  // 订阅
                    @Override
                    public void onNext(@NonNull TestBean expressInfo) {
                        expressView.updateView(expressInfo);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        expressView.showError(e.getMessage());
                        expressView.hideProgressDialog();
                    }

                    @Override
                    public void onComplete() {
                        expressView.hideProgressDialog();
                    }
                });
    }
}
